package com.zxgllhh.run;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import com.zxgllhh.db.DataSourceFactory;
import com.zxgllhh.domain.Field;
import com.zxgllhh.domain.Table;
import com.zxgllhh.service.TableManager;

public class TableConnectionThread extends Thread {

	private static int count = 0;

	private List<String> tableList = new ArrayList<String>();

	public void run() {
		count++;
		
		try {
			for (String tableName : tableList) {
				Connection connection = DataSourceFactory.getConnectionByDataSource();
				connection.setAutoCommit(true);
				connection.setReadOnly(true);
				
				TableManager tm = new TableManager();
				Table t = new Table();
				t.setTableName(tableName);
				List<Field> fieldList = tm.getFieldListByTableName(connection,tableName);
				t.setFieldList(fieldList);
				List<String> primaryKeyList = tm.getPrimaryKeyListByTableName(connection, tableName);
				t.setPrimaryKeyList(primaryKeyList);
				connection.close();
				com.zxgllhh.domain.ConnectionContext.getTableList().add(t);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public List<String> getTableList() {
		return tableList;
	}

	public void setTableList(List<String> tableList) {
		this.tableList = tableList;
	}

}
